﻿using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using LuKou.Web.Controllers.Base;
using LuKou.Member.Domains;
using LuKou.Member;
using System;

namespace LuKou.Web.UserAuth
{
    public class WebAuthorizeAttribute : FilterAttribute, IAuthorizationFilter
    {
        /// <summary>
        /// 是否管理后台
        /// </summary>
        public bool IsManager { get; set; }

        /// <summary>
        /// 用户类型
        /// </summary>
        public WebUserType UserType { get; set; }

        public WebAuthorizeAttribute()
        {
            this.IsManager = false;
        }

        private bool CheckRole(UserSession session)
        {
            if (null == session)
            {
                return false;
            }

            bool isCheck = false;
            switch (session.UserType)
            {
                case (int) EUserType.Vendor:
                    isCheck =(this.UserType & WebUserType.Seller) == WebUserType.Seller;
                    break;
                case (int)EUserType.Agents:
                    isCheck = (this.UserType & WebUserType.Agents) == WebUserType.Agents;
                    break;
                case (int)EUserType.Photography:
                    isCheck = (this.UserType & WebUserType.Photography) == WebUserType.Photography;
                    break;
                case (int)EUserType.Shipped:
                    isCheck = (this.UserType & WebUserType.Shipped) == WebUserType.Shipped;
                    break;
                case (int)EUserType.System:
                    isCheck = (this.UserType & WebUserType.System) == WebUserType.System;
                    break;
            }

            return isCheck;
        }

        #region IAuthorizationFilter 成员

        public void OnAuthorization(AuthorizationContext filterContext)
        {
            string referrerUrl = filterContext.HttpContext.Request.Url.AbsoluteUri;
            UserSession session = (filterContext.Controller as BaseController).UserSession;

            if (!CheckRole(session))
            {
                if (this.IsManager)
                {
                    filterContext.Result = new RedirectToRouteResult("Manage", new RouteValueDictionary(new
                    {
                        controller = "Login",
                        action = "Index",
                        ReferrerUrl = referrerUrl
                    }));
                    return;
                }
                else
                {
                    filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { 
                        controller = "Account", action = "Login", ReferrerUrl = referrerUrl 
                    }));
                    return;
                }
            }
        }
        #endregion
    }

    /// <summary>
    /// 用户类型
    /// </summary>
    [Flags]
    public enum WebUserType
    {
        /// <summary>
        /// 系统管理员
        /// </summary>
        System,

        /// <summary>
        /// 厂商
        /// </summary>
        Seller,

        /// <summary>
        /// 代理
        /// </summary>
        Agents,

        /// <summary>
        /// 摄影店
        /// </summary>
        Photography,

        /// <summary>
        /// 代发
        /// </summary>
        Shipped,
    }
}
